package com.zzq.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.zzq.constant.MessageConstant;
import com.zzq.constant.RedisConstant;
import com.zzq.entity.PageResult;
import com.zzq.entity.QueryPageBean;
import com.zzq.entity.Result;
import com.zzq.pojo.Setmeal;
import com.zzq.service.SetmealService;
import com.zzq.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

/**
 * 幼儿猿大班
 * 2021/7/23
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return setmealService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    public Result delete(Integer id){
        try {
            setmealService.delete(id);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        Jedis jedis =null;
        try {
            // 1.获取文件的原始名称
            String originalFilename = imgFile.getOriginalFilename();
            // 2.生成不重复的文件名
            String filename= UUID.randomUUID().toString()+originalFilename;

            jedis = jedisPool.getResource();

            // 3.将文件名存到redis
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,filename);

            // 4.将文件上传到七牛云
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),filename);

            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,filename);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }finally {
            jedis.close();
        }
    }

    @RequestMapping("/save")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result save(@RequestBody Setmeal setmeal,Integer[] travelgroupIds){
        try {
            setmealService.save(setmeal,travelgroupIds);
            return new Result(true,MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.ADD_TRAVELGROUP_FAIL);
        }

    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal=setmealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findSetmealIdTravelGroupId")
    public Result findSetmealIdTravelGroupId(Integer id){
        try {
            List<Integer> list= setmealService.findSetmealIdTravelGroupId(id);
            return new Result(true,null,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null);
        }
    }

    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Result update(Integer[] travelgroupIds,@RequestBody Setmeal setmeal){
        try {
            setmealService.update(travelgroupIds,setmeal);
            return  new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }
}
